package com.mszlu.news.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mszlu.news.dao.mapper.FollowMapper;
import com.mszlu.news.dao.mapper.SysUserMapper;
import com.mszlu.news.dao.pojo.Follow;
import com.mszlu.news.dao.pojo.SysUser;
import com.mszlu.news.service.FollowService;
import com.mszlu.news.vo.FollowVo;
import com.mszlu.news.vo.Result;
import com.mszlu.news.vo.params.FollowParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class FollowServiceImpl implements FollowService {
    @Autowired
    private FollowMapper followMapper;
    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public Result listFanByUserId(Long userId) {
        List<FollowVo> followVoList=followMapper.listFanByUserId(userId);
        return Result.success(followVoList);
    }

    @Override
    public Result listFollowByUserId(Long userId) {
        List<FollowVo> followVoList=followMapper.listFollowByUserId(userId);
        return Result.success(followVoList);
    }

    @Override
    public Result addFollow(FollowParam followParam) {
        Follow follow=new Follow();
        follow.setFollowId(followParam.getFollowId());
        follow.setFanId(followParam.getFanId());
        followMapper.insert(follow);
        SysUser sysUser=sysUserMapper.selectById(followParam.getFollowId());
        int FanCounts=sysUser.getFanCounts();
        sysUser.setId(followParam.getFollowId());
        sysUser.setFanCounts(FanCounts+1);
        sysUserMapper.updateById(sysUser);
        SysUser sysUser1=sysUserMapper.selectById(followParam.getFanId());
        int FollowCounts=sysUser1.getFollowCounts();
        sysUser1.setId(followParam.getFanId());
        sysUser1.setFollowCounts(FollowCounts+1);
        sysUserMapper.updateById(sysUser1);
        return Result.success("关注成功");
    }

    @Override
    public Result deleteFollow(FollowParam followParam) {
        Map<String,Object> columnMap=new HashMap<>();
        columnMap.put("follow_id",followParam.getFollowId());
        columnMap.put("fan_id",followParam.getFanId());
        followMapper.deleteByMap(columnMap);
        SysUser sysUser=sysUserMapper.selectById(followParam.getFollowId());
        int FanCounts=sysUser.getFanCounts();
        sysUser.setId(followParam.getFollowId());
        sysUser.setFanCounts(FanCounts-1);
        sysUserMapper.updateById(sysUser);
        SysUser sysUser1=sysUserMapper.selectById(followParam.getFanId());
        int FollowCounts=sysUser1.getFollowCounts();
        sysUser1.setId(followParam.getFanId());
        sysUser1.setFollowCounts(FollowCounts-1);
        sysUserMapper.updateById(sysUser1);
        return Result.success("取消关注成功");
    }

    @Override
    public Result Counts(Long userId) {
        QueryWrapper<Follow> query = new QueryWrapper<>();
        query.eq("follow_id",userId);
        int FanCounts=followMapper.selectCount(query);
        QueryWrapper<Follow> followQueryWrapper=new QueryWrapper<>();
        followQueryWrapper.eq("fan_id",userId);
        int FollowCounts=followMapper.selectCount(followQueryWrapper);
        SysUser sysUser=new SysUser();
        sysUser.setId(userId);
        sysUser.setFollowCounts(FollowCounts);
        sysUser.setFanCounts(FanCounts);
        sysUserMapper.updateById(sysUser);
        Map<String,Object> columnMap=new HashMap<>();
        columnMap.put("followCounts",FollowCounts);
        columnMap.put("fanCounts",FanCounts);
        return Result.success(columnMap);
    }
}
